﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using FastReport;
using System.Data;
using System.ServiceModel;
using System.Xml;

using WCFMINERP.InnerFactory.IView;
using WCFMINERP.InnerFactoryContracts.DataContracts;
using WCFMINERP.InnerFactoryContracts.InterFaces;
using WCFMINERP.COM;

namespace WCFMINERP.InnerFactory.Presenter
{
    public class MainPresenter
    {

        ChannelFactory<IInnerFactory> _proxy;
        IInnerFactory _innerFactory;

        public IView.IFormMain _IView;
        public MainPresenter(IFormMain inIForm)
        {
            _IView = inIForm;
            _proxy = new ChannelFactory<IInnerFactory>("IInnerFactory");
            _innerFactory = _proxy.CreateChannel();
            //_IView.FactoryId = 2;//测试用
        }
        public void initForm()
        {
            List<ProduceView> produceList = _innerFactory.NewGetProduceViewList(_IView.FactoryId);
            _IView.DisplayList(produceList);
        }
        public ProduceView GetViewbyBphotoBar(string inPhotoBarcode)
        {
            ProduceView view = _innerFactory.GetProduceViewByPhotoCode(inPhotoBarcode);
            
            return view;
        }
        /// <summary>
        /// 
        /// </summary>
        public void StartProduce()
        {
            //OutProcessRecord_Info info = _bll.GetReCordByCode(_IView.CurrBarCode);
            OutProcessRecord_Info info = _innerFactory.GetReCordByCode(_IView.CurrBarCode);
            if (info == null)
            {
                MessageBox.Show("请确认"+_IView.CurrBarCode+"存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (info.Status == (int)OutProcessRecordStatus.InGrid)
            {
                MessageBox.Show("产品" + _IView.CurrBarCode + "在格子中未打印", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (info.Status == (int)OutProcessRecordStatus.OutWaitFenDui)
            {
                MessageBox.Show("产品" + _IView.CurrBarCode + "未分配格子", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
           
            //if (info.Status != (int)OutProcessRecordStatus.Outing)
            //{
            //    MessageBox.Show("产品"+_IView.CurrBarCode+"已完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            else
            {
                if (info.Status2 == (int)OutProcessRecordStatus2.Wait)
                {
                    //打出条码
                    //Report report = new Report();
                    //bool isBarcode = Properties.Settings.Default.IsBarCode;
                    //if (isBarcode)
                    //{
                    //    int Num = Properties.Settings.Default.intBarCode;

                    //    //创建XmlData
                    //    DataTable barDT = CreateDT();

                    //    for (int i = 0; i < Num; i++)
                    //    {
                    //        barDT.Rows.Add(_IView.CurrBarCode);
                    //    }
                    //    barDT.TableName = "barDT";
                    //    XmlDocument doc = new XmlDocument();
                    //    barDT.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
                    //    //report.RegisterData(barDT, "barDT");
                    //    report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
                    //    report.Show();
                    //    report.Print();


                    //}
                    //PrintBarCode();
                    bool isBarcode = Properties.Settings.Default.IsBarCode;
                    if (isBarcode)
                    {
                        PrintBarCode();
                    }

                    //开始生产
                    _innerFactory.StartProduce(InnerFactoryParameter.UserInfo.UserId, info.OutProcessRecordId);
                    
                    //_IView.DisplayList(_bll.GetProduceViewList(_IView.FactoryId));
                    MessageBox.Show("产品" + _IView.CurrBarCode + "激活成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //PrintBarCode();
                    //initForm();
                    return;
                }
                else if (info.Status2 == (int)OutProcessRecordStatus2.Doing)
                {
                    //结束生产
                    if (MessageBox.Show("确认" + _IView.CurrBarCode + "生产完成?确认请按'确定'，否则请点击'取消'", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        _innerFactory.EndProduce(InnerFactoryParameter.UserInfo.UserId, info.OutProcessRecordId);
                        MessageBox.Show("产品" + _IView.CurrBarCode + "结束生产，请将标签贴到产品上！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        PrintBarCode();
                        initForm();                       
                        return;
                    }
                }
                else if (info.Status2 == (int)OutProcessRecordStatus2.Error)
                {
                    //恢复生产
                    if (MessageBox.Show("确认继续生产该返工产品（" + _IView.CurrBarCode + "）?确认请点击'确定'，否则请点击'取消'", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        _innerFactory.RebootProduce(InnerFactoryParameter.UserInfo.UserId, info.OutProcessRecordId);
                        MessageBox.Show("产品" + _IView.CurrBarCode + "恢复生产！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
                        initForm();
                        return;
                    }
                }
                else
                {
                    if (DialogResult.OK == MessageBox.Show("产品" + _IView.CurrBarCode + "已生产完成！是否重新打印不干胶标签？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                    {
                        PrintBarCode();
                    }
                    else
                    {

                        return;
                    }
                }
            }
        }

        public void PrintBarCode()
        {
            ////打印当前的条码
            //Report report = new Report();
            //bool isBarcode = Properties.Settings.Default.IsBarCode;
            //if (isBarcode)
            //{
            //    int Num = Properties.Settings.Default.intBarCode;

            //    //创建XmlData
            //    DataTable barDT = CreateDT();

            //    for (int i = 0; i < Num; i++)
            //    {
            //        barDT.Rows.Add(_IView.CurrBarCode);
            //    }
            //    barDT.TableName = "barDT";
            //    XmlDocument doc = new XmlDocument();
            //    barDT.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            //    //report.RegisterData(barDT, "barDT");
            //    report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            //    //report.Show();
            //    report.Print();
            //}
            Report report = new Report();
            DataTable table = _innerFactory.GetPrintInfo(_IView.CurrBarCode);
            XmlDocument doc = new XmlDocument();
            table.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            report.PrintSettings.ShowDialog = false;
            //report.Show();
            report.Print();
          
        }
        public void PrintBarCodeByPhotoCode(string code)
        {
            
            Report report = new Report();
            DataTable table = _innerFactory.GetPrintInfoByPhoto(code);
            if (table == null)
            {
                MessageBox.Show("产品不存在或者条码输入错误，请重新输入");
                return;
            }
            XmlDocument doc = new XmlDocument();
            table.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            report.PrintSettings.ShowDialog = false;
            //report.Show();
            report.Print();

        }
        public void PrintBarCode(string barcode)
        {

            Report report = new Report();
            DataTable table = _innerFactory.GetPrintInfo(barcode);
            if (table == null)
            {
                MessageBox.Show("产品不存在或者条码输入错误，请重新输入");
                return;
            }
            XmlDocument doc = new XmlDocument();
            table.WriteXml(@"C:\Windows\xmldata\barDT.xml", XmlWriteMode.WriteSchema);
            report.Load(Application.StartupPath + "\\FactoryBarCode.frx");
            report.PrintSettings.ShowDialog = false;
            //report.Show();
            report.Print();

        }


        /// <summary>
        /// 创建DT
        /// </summary>
        /// <returns></returns>
        public DataTable CreateDT()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BarCode", typeof(string));
            return dt;
        }


        public OutProcessShop_Info GetOutShopInfoById(int outShopId)
        {
            return _innerFactory.GetOutShopById(outShopId);
        }
    }
}
